package Registration;

import java.io.RandomAccessFile;
import java.sql.ResultSet;
import dbmanager.ChekInMaster;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class AppendItemcode
{
	String Path;
		DBManager db = new DBManager();
		GetIdName gin;
		
		public AppendItemcode(String Path)
		{
		
			this.Path = Path;
			db.getConnect();
			gin = new  GetIdName(db);
		}
		public int Append()
		{
				int Count=0;
				
				try 
				{
					RandomAccessFile raf = new RandomAccessFile(Path,"rws");
					RandomAccessFile output = new RandomAccessFile("D:\\Registration\\Item\\CODE\\RateList.txt","rws");
					RandomAccessFile error = new RandomAccessFile("D:\\Registration\\Item\\CODE\\Error.txt","rws");
					
					String line = null;
					line=raf.readLine();
					String[] records = line.split("\t");
					output.writeBytes(line+"\t"+"Itemcode"+"\n");
					while((line=raf.readLine())!=null)
					{
						try 
						{
							records = line.split("\t");
							String mid = gin.getId(records[0].trim());
							
							if(mid.equalsIgnoreCase("no$")==false)
							{
								ResultSet rs = db.getSelect("SELECT pv FROM property_details where mid='"+mid+"' and pid="+gin.getId("itemcode"));
								rs.next();
								String Itemcode = gin.getItem(rs.getString("pv"));
								line = line + "\t"+Itemcode+"\n";
							}
							else
								line = line + "\t"+"null"+"\n";
							
							System.out.println("MESSGE IS :"+line);
							output.writeBytes(line);
							Count++;
						} 
						catch (Exception e) 
						{
							error.writeBytes(line+"\n");
						}
					}
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				return Count;
			}
			public static void main(String[] args) 
			{
				AppendItemcode reg = new AppendItemcode("D:\\Register\\Item\\PRICELIST.csv");
				System.out.println("Result :: "+reg.Append()+" Items are Registered in the Database");
			}
	
}
